package com.np.intentdemo2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("IntentReset")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 打开新的 Activity
        findViewById(R.id.button_00).setOnClickListener(v -> {
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
        });

        // 打开默认浏览器并在默认搜索引擎搜索指定内容
        findViewById(R.id.button_01).setOnClickListener(v -> {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_WEB_SEARCH);
            i.putExtra(SearchManager.QUERY, "MAGA");
            startActivity(i);
        });

        // 打开默认浏览器并访问指定网址
        findViewById(R.id.button_02).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
            startActivity(i);
        });

        // 打开默认地图引用并定位到指定坐标
        findViewById(R.id.button_03).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:38.899533,-77.036476"));
            startActivity(i);
        });

        // 路径规划
        findViewById(R.id.button_04).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"));
            startActivity(i);
        });

        // 拨打电话
        findViewById(R.id.button_05).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:10086"));
            startActivity(i);
        });

        // 发送短信（1）
        findViewById(R.id.button_06).setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.putExtra("sms_body", "hello world!");
            i.setType("vnd.android-dir/mms-sms");
            startActivity(i);
        });

        // 发送短信（2）
        findViewById(R.id.button_07).setOnClickListener(v -> {
            int way = 0;

            if (way == 0) {
                // todo: 程序运行报错
                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13500000000"));
                i.putExtra("sms_body", "hello world!");
                i.setType("vnd.android-dir/mms-sms");
                startActivity(i);
            } else if (way == 1) {
                // todo: 找不到 Messaging
//                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", "13500000000", null));
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, "hello world!");
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
//                startActivity(i);
            }
        });

        // 发送彩信
        findViewById(R.id.button_08).setOnClickListener(v -> {
            int way = 0;

            if (way == 0) {
                Intent i = new Intent(Intent.ACTION_SEND, Uri.parse("content://media/external/images/media/23"));
                i.putExtra("sms_body", "some text");
                i.setType("image/png");
                startActivity(i);
            } else if (way == 1) {
//                StringBuilder sb = new StringBuilder();
//                sb.append("file://");
//                sb.append(fd.getAbsoluteFile());
//                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
//
//                // Below extra datas are all optional.
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
//                i.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
//                startActivity(i);
            }
        });

        // 发送电子邮件
        findViewById(R.id.button_09).setOnClickListener(v -> {
            int way = 1;

            if (way == 0) {
                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(" mailto:xxx@abc.com"));
                startActivity(i);
            } else if (way == 1) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_EMAIL, " me@abc.com");
                i.putExtra(Intent.EXTRA_TEXT, "The email body text");
                i.setType("text/plain");
                startActivity(Intent.createChooser(i, "Choose Email Client"));
            } else if (way == 2) {
                Intent i = new Intent(Intent.ACTION_SEND);
                String[] tos = {" me@abc.com"};
                String[] ccs = {" you@abc.com"};
                i.putExtra(Intent.EXTRA_EMAIL, tos);
                i.putExtra(Intent.EXTRA_CC, ccs);
                i.putExtra(Intent.EXTRA_TEXT, "The email body text");
                i.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
                i.setType("message/rfc822");
                startActivity(Intent.createChooser(i, "Choose Email Client"));
            } else if (way == 3) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
                i.putExtra(Intent.EXTRA_STREAM, " file:///sdcard/mysong.mp3");
                i.setType("audio/mp3");
                startActivity(Intent.createChooser(i, "Choose Email Client"));
            }
        });

        // 播放多媒体
        findViewById(R.id.button_10).setOnClickListener(v -> {
            int way = 1;

            if (way == 0) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.parse(" file:///sdcard/song.mp3");
                i.setDataAndType(uri, "audio/mp3");
                startActivity(i);
            } else if (way == 1) {
                Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
                Intent i = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(i);
            }
        });

        // 卸载 apk
        findViewById(R.id.button_11).setOnClickListener(v -> {
//            Uri uri = Uri.fromParts("package", strPackageName, null);
//            Intent i = new Intent(Intent.ACTION_DELETE, uri);
//            startActivity(i);
        });

        // 安装 apk
        findViewById(R.id.button_12).setOnClickListener(v -> {
//            Uri installUri = Uri.fromParts("package", "xxx", null);
//            returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
        });

        // 打开相机
        findViewById(R.id.button_13).setOnClickListener(v -> {
            int way = 0;

            if (way == 0) {
                Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
                this.sendBroadcast(i);
            } else if (way == 1) {
                long dateTaken = System.currentTimeMillis();
                String name = dateTaken + ".jpg";
                String folder = "";
                String fileName = folder + name;
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, fileName);
                values.put("_data", fileName);
                values.put(MediaStore.Images.Media.PICASA_ID, fileName);
                values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
                values.put(MediaStore.Images.Media.DESCRIPTION, fileName);
                values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);
                Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(i, 10);
            }
        });

        // 从gallery选取图片
        findViewById(R.id.button_14).setOnClickListener(v -> {
            Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(i, 11);
        });

        // 打开录音机
        findViewById(R.id.button_15).setOnClickListener(v -> {
            Intent i = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            startActivity(i);
        });

        // 打开应用商店并展示指定应用的详情
        findViewById(R.id.button_16).setOnClickListener(v -> {

            Uri uri = Uri.parse("market://details?id=app_id");
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(i);
            //where app_id is the application ID, find the ID
            //by clicking on your application on Market home
            //page, and notice the ID from the address bar

            // 刚才找app id未果，结果发现用package name也可以，这个简单多了
            //Uri uri = Uri.parse("market://details?id=<packagename>");
        });

        // 打开应用商店并搜索指定应用
        findViewById(R.id.button_17).setOnClickListener(v -> {
            Uri uri = Uri.parse("market://search?q=pname:pkg_name");
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(i);
            //where pkg_name is the full package path for an application
        });

        // 打开联系人列表
        findViewById(R.id.button_18).setOnClickListener(v -> {
            int REQUEST_TEXT = 1;
            int way = 0;

            if (way == 0) {
                Intent i = new Intent();
                i.setAction(Intent.ACTION_GET_CONTENT);
                i.setType("vnd.android.cursor.item/phone");
                startActivityForResult(i, REQUEST_TEXT);
            } else if (way == 1) {
                Uri uri = Uri.parse("content://contacts/people");
                Intent i = new Intent(Intent.ACTION_PICK, uri);
                startActivityForResult(i, REQUEST_TEXT);
            }
        });

        // 打开另一程序
        findViewById(R.id.button_19).setOnClickListener(v -> {
            Intent i = new Intent();
            ComponentName cn = new ComponentName("com.yellowbook.android2", "com.yellowbook.android2.AndroidSearch");
            i.setComponent(cn);
            i.setAction("android.intent.action.MAIN");
            startActivityForResult(i, RESULT_OK);
        });

        // 调用系统编辑添加联系人（高版本SDK有效）
        findViewById(R.id.button_20).setOnClickListener(v -> {
//            Intent i = newIntent(Intent.ACTION_INSERT_OR_EDIT);
//            i.setType("vnd.android.cursor.item/contact");
//            //i.setType(Contacts.CONTENT_ITEM_TYPE);
//            i.putExtra("name", "myName");
//            i.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, "organization");
//            i.putExtra(android.provider.Contacts.Intents.Insert.EMAIL, "email");
//            i.putExtra(android.provider.Contacts.Intents.Insert.PHONE, "homePhone");
//            i.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE, "mobilePhone");
//            i.putExtra(android.provider.Contacts.Intents.Insert.TERTIARY_PHONE, "workPhone");
//            i.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE, "title");
//            startActivity(i);
        });

        // 调用系统编辑添加联系人（全有效）
        findViewById(R.id.button_21).setOnClickListener(v -> {
//            Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT);
//            intent.setType(People.CONTENT_ITEM_TYPE);
//            intent.putExtra(Contacts.Intents.Insert.NAME, "My Name");
//            intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
//            intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
//            intent.putExtra(Contacts.Intents.Insert.EMAIL, " com@com.com");
//            intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK);
//            startActivity(intent);
        });
    }
}